﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WIKID.Model;
using System.Xml;

namespace WIKID.Accessor
{
    class XMLWriter
    {
        /// <summary>
        /// Update Scores and Lectures
        /// </summary>
        /// <param name="accountModel"></param>
        public void updateXML(AccountModel accountModel)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlNode xmlRoot = doc.CreateElement("account");
            doc.InsertBefore(xmlDeclaration, doc.DocumentElement);
            doc.AppendChild(xmlRoot);

            String fileName = accountModel.AccId + "_" + accountModel.AccName;
            String id = fileName.Substring(0, fileName.IndexOf("_"));

            XmlAttribute attribute = doc.CreateAttribute("accId");
            attribute.Value = id;
            xmlRoot.Attributes.Append(attribute);

            XmlElement accountNameNode = doc.CreateElement("accName");
            accountNameNode.InnerText = accountModel.AccName;
            xmlRoot.AppendChild(accountNameNode);

            XmlElement accountPicNode = doc.CreateElement("accPic");
            accountPicNode.InnerText = accountModel.AccPic;
            xmlRoot.AppendChild(accountPicNode);

            XmlElement accountAgeNode = doc.CreateElement("accAge");
            accountAgeNode.InnerText = accountModel.AccAge.ToString();
            xmlRoot.AppendChild(accountAgeNode);

            XmlElement passIdNode = doc.CreateElement("passId");
            passIdNode.InnerText = id;
            xmlRoot.AppendChild(passIdNode);

            XmlElement passPicNode = doc.CreateElement("passPic");
            passPicNode.InnerText = accountModel.PassPic;
            xmlRoot.AppendChild(passPicNode);

            List<ScoreModel> scoreModels = accountModel.Scores;
            if (scoreModels.Count != 0)
            {
                XmlElement scoreStoreNode = doc.CreateElement("scoreStore");
                for (int i = 0; i < scoreModels.Count; i++)
                {
                    XmlElement scoreNode = doc.CreateElement("score");
                    XmlElement lectureIdNode = doc.CreateElement("lectureId");
                    XmlElement studyingScoreNode = doc.CreateElement("studyingScore");
                    XmlElement attitudeScoreNode = doc.CreateElement("attitudeScore");
                    XmlElement dateNode = doc.CreateElement("date");

                    lectureIdNode.InnerText = scoreModels[i].LectureId;
                    studyingScoreNode.InnerText = scoreModels[i].StudyingScore.ToString();
                    attitudeScoreNode.InnerText = scoreModels[i].StudyingScore.ToString();
                    dateNode.InnerText = scoreModels[i].Date.ToString();

                    scoreNode.AppendChild(lectureIdNode);
                    scoreNode.AppendChild(studyingScoreNode);
                    scoreNode.AppendChild(attitudeScoreNode);
                    scoreNode.AppendChild(dateNode);

                    scoreStoreNode.AppendChild(scoreNode);
                }
                xmlRoot.AppendChild(scoreStoreNode);
            }

            List<ScheduleModel> scheduleModels = accountModel.Schedules;
            if (scheduleModels.Count != 0)
            {
                XmlElement scheduleStoreNode = doc.CreateElement("scheduleStore");
                for (int i = 0; i < scheduleModels.Count; i++)
                {
                    XmlElement scheduleNode = doc.CreateElement("schedule");
                    XmlElement lectureIdNode = doc.CreateElement("lectureId");
                    XmlElement startDateTimeNode = doc.CreateElement("startDateTime");
                    XmlElement endDateTimeNode = doc.CreateElement("endDateTime");

                    lectureIdNode.InnerText = scheduleModels[i].LectureId;
                    startDateTimeNode.InnerText = scheduleModels[i].StartDateTime.ToString();
                    endDateTimeNode.InnerText = scheduleModels[i].EndDateTime.ToString();

                    scheduleNode.AppendChild(lectureIdNode);
                    scheduleNode.AppendChild(startDateTimeNode);
                    scheduleNode.AppendChild(endDateTimeNode);

                    scheduleStoreNode.AppendChild(scheduleNode);
                }
                xmlRoot.AppendChild(scheduleStoreNode);
            }

            string filename = @"Accounts\" + fileName + ".xml";
            System.IO.File.Delete(fileName);
            doc.Save(filename);
        }
    }
}
